package hittegods.classes;

import hittegods.database.MySQLConn;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;

public class Search {

    public final static int SUCCESS = 1;
    public final static int ERROR = 2;
    public final static int SQL_EXCEPTION = 3;
    public final static int UNDEFINED_ERROR = 4;
    public static final Object[] HEADERS = {"Sagsnr", "Kategori", "Funden dato", "Findested", "Finders navn", "Ejers navn"};

    public static Item[] search(HashMap<String, String> srcMap) {
        String srcStr = "SELECT caseNo FROM item WHERE ";
        boolean first = false;
        Item[] items = null;
        Item item = null;
        if (srcMap.containsKey("caseNo")) {
            items = new Item[1];
            items[0] = new Item((String) srcMap.get("caseNo"));
        } else {
            try {
                if (srcMap.containsKey("categoryId")) {
                    srcStr += "categoryId = '" + srcMap.get("categoryId") + "'";
                    first = true;
                }
                if (srcMap.containsKey("foundDate")) {
                    if (first) {
                        srcStr += " AND ";
                    }
                    srcStr += "foundDate = '" + srcMap.get("foundDate") + "'";
                }
                if (srcMap.containsKey("foundPlace")) {
                    if (first) {
                        srcStr += " AND ";
                    }
                    srcStr += "foundPlace = '" + srcMap.get("foundPlace") + "'";
                }
                if (srcMap.containsKey("finderName")) {
                    if (first) {
                        srcStr += " AND ";
                    }
                    srcStr += "finderName = '" + srcMap.get("finderName") + "'";
                }
                if (srcMap.containsKey("ownerName")) {
                    if (first) {
                        srcStr += " AND ";
                    }
                    srcStr += "ownerName = '" + srcMap.get("ownerName") + "'";
                }
                System.out.println(srcStr);
                Statement stmt = MySQLConn.getConnection().createStatement();
                ResultSet rs = stmt.executeQuery(srcStr);
                System.out.println("isClosed " + rs.isClosed());
                ArrayList<String> cases = new ArrayList<String>();
                while (rs.next()) {
                    String caseN = rs.getString("caseNo");
                    cases.add(caseN);
                }
                rs.close();
                items = new Item[cases.size()];
                for (int i = 0; i < cases.size(); i++) {
                    items[i] = new Item(cases.get(i));
                }
            } catch (Exception ex) {
            }
        }
        return items;
    }

    public static void sort(Item[] unsorted, int sortAtt, boolean desc) {
        Arrays.sort(unsorted, new Item.ItemComparator(sortAtt));
        if (desc) {
            List<Item> temp = Arrays.asList(unsorted);
            Collections.reverse(temp);
            unsorted = (Item[]) temp.toArray();
        }
    }

    public static Object[][] parseItemArray(Item[] items) {
        System.out.println("parseItemArray - Item count "+items.length);
        Object[][] result = new Object[items.length][6];
        for (int i = 0; i < items.length; i++) {
            System.out.println("Parse 1 run");
            result[i][0] = items[i].getCaseNo();
            result[i][1] = items[i].getCategoryId();
            result[i][2] = items[i].getFoundDate();
            result[i][3] = items[i].getFoundPlace();
            result[i][4] = items[i].getFinderName();
            result[i][5] = items[i].getOwnerName();
        }
        return result;
    }
}
